const carModel = require('../model/carModel.js');

const carController =  {
    GetCarList: async (req, res) => {
        console.log(req.body,'controller层获取汽车列表调试');
        try {
            const {list,total} = await carModel.GetCarList(req.body);
            console.log(list,total,'汽车列表响应参数');
            if(list.length > 0){
                res.status(200).send({
                    code: 1,
                    msg: "成功获取汽车列表",
                    list,
                    total,
                });
            }else{
                res.status(404).send({
                    code: 0,
                    msg: "获取汽车列表失败，请输入正确的参数",
                });
            }
        } catch (error) {
            console.log(error,"获取汽车列表出现error");
        };
    },
    AddCar: async (req, res) => {
        try {
            const result = await carModel.AddCar(req.body,req.file);
            console.log(result,'添加汽车结果');
            if(result == 0){
                res.send({
                    code:0,
                    msg:"汽车型号已存在，请添加未有的型号汽车"
                });
            }else if(result == 1){
                res.send({
                    code:1,
                    msg:"添加汽车成功！"
                });
            }else if(result.length == 0){
                res.send({
                    code:2,
                    msg:"输入参数有误，请稍后再试！"
                });
            }
        } catch (error) {
            console.log(error,'添加汽车出错啦！');
        }
    },
    EditCar :async (req,res) => {
        try {
            const result = await carModel.EditCar(req.body,req.file);
            if(result === 1){
                res.send({
                    code:1,
                    msg:"修改汽车成功！"
                });
            }else {
                res.send({
                    code:2,
                    msg:"输入参数有误，请稍后再试！"
                });
            }
        } catch (error) {
            console.log(error,'修改汽车出错啦！');
        }
    },
    DeleteCar: async (req, res) => {
        try {
            const result = await carModel.DeleteCar(req.body);
            if (result) {
                res.status(200).send({
                    code: 1,
                    msg: "删除汽车成功",
                });
            } else {
                res.status(404).send({
                    code: 0,
                    msg: "删除汽车失败",
                });
            }
        } catch (error) {
            console.log(error, '删除汽车出现error');
        };
    },
    BatchDeleteCar: async (req,res)=>{
        try {
            let result = await carModel.BatchDeleteCar(req.body);
            if(result.affectedRows !== 0){
                res.status(200).send({
                    code:1,
                    msg:"批量删除汽车成功",
                });
            }else{
                res.status(404).send({
                    code:0,
                    msg:"批量删除汽车失败",
                });
            }
        }catch (error){
            console.log(error,'controller层批量删除汽车出现error');
        }
    },
    GetCarDetail:async (req,res) => {
        try{
            const result = await carModel.GetCarDetail(req.params);
            console.log(result,'car-controller结果');
            if(result.length >0){
                res.status(200).send({
                    code:1,
                    detail:result[0],
                    msg:"获取汽车详情成功"
                })
            }else{
                res.status(404).send({
                    code:0,
                    msg:"获取汽车详情失败",
                });
            }
        }catch (error){
            console.log(error,'获取汽车详情出现error');
        }
    }
};

module.exports = carController;